/**
 * *******************************************
 * user: zmq
 * describe: connector服务器 负责玩家登录登出
 * created:
 * date: 2021/8/18
 * *******************************************
 */

import {Application, FrontendSession} from 'pinus';
import DataApi from "../../../util/dataApi";
import connectorService from "../../../service/connectorService";


// 默认导出方式
export default function (app: Application) {
    return new ConnectorHandler(app);
}

export class ConnectorHandler {

    constructor(private app: Application) {
        this.app = app;
    }

    /**
     * 创建角色api
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    public async createRole(msg: any, session: FrontendSession) {

        let self = this;
        let createMsg = {          // 参数赋值
            account: msg.account,
            sex: msg.sex,
            nickname: msg.nickname,
        };

        // Service 专门处理逻辑
        return await connectorService.createRole(self.app, session, createMsg);

    }


    /**
     * 角色登录api      这边数值传递，其他逻辑在Service中处理
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    public async login(msg: any, session: FrontendSession) {

        let self = this;
        let loginMsg  = {
            uid: msg.uid,
            account: msg.account,
        };
        // Service 专门处理逻辑
        return await connectorService.login(self.app, session, loginMsg);
    }

    /**
     * 玩家主动退出登录接口
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    public async logout(msg: any, session: FrontendSession) {

        let self = this;
        let uid = session.uid;

        if (!uid) {         //
            return {code: DataApi.errCode("UN_LOGIN_USER")};
        }

        // 退出登录,走离线监听
        self.app.get('sessionService').kick(uid);

        return {msg: ""};
    }

}